// pages/my/my.js
var klassModel = require('../../models/klass.js')
var userModel = require('../../models/user.js')
var bxt = require('../../utils/bxt.js')
var request = require('../../operation/operation.js')
var util = require('../../utils/util.js')
var noticeModel = require('../../models/notice.js')

Page({

  /**
   * 页面的初始数据
   */
  data: {
    // klass:null,
    klasses:[],
    showPassword:false,
    totalNoticeCount: 0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.initClassView()
    // this.initRoleView()
    // this.initMyNoticesView()
    this.getUserInfo()
    // this.getNotices()

    getApp().notificationCenter.register(bxt.BIND_ROLE_MESSAGE, this, "getUserInfo")
    getApp().notificationCenter.register(bxt.DELETE_KLASS_MESSAGE, this, "getUserInfo")
    getApp().notificationCenter.register(bxt.UPDATE_KLASS_MESSAGE, this, "getUserInfo")
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    getApp().globalData.currentPage = '../my/my'
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    getApp().notificationCenter.remove(bxt.BIND_ROLE_MESSAGE,this);
    getApp().notificationCenter.remove(bxt.DELETE_KLASS_MESSAGE, this);
    getApp().notificationCenter.remove(bxt.UPDATE_KLASS_MESSAGE, this);
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.getUserInfo()
    // this.getNotices()
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  // onShareAppMessage: function (options) {
  //   return noticeModel.showActivateClass(this, options)
  // },

  /**
   * 初始化班级UI信息
   */
  initClassView:function() {
    // var klass = klassModel.getKlassUIData()
    // var user = userModel.getCurrentUser()
    // var showPassword = false
    // if (klass.creatorSid == user.sid && (null == klass.wxGroupUid || '' == klass.wxGroupUid)) { // 班级创建者，同时未绑定微信群时才显示
    //   showPassword = true
    // }
    // this.setData({
    //   klass: klass,
    //   showPassword: showPassword
    // })
    let klasses = klassModel.getAllKlassUIData()
    if (klasses) {      
      for (let index = 0, size = klasses.length; index < size; index++) {
        klasses[index].currentUserRoleName = userModel.getKlassRoleName(klasses[index])
      }

      this.setData({
        klasses: klasses
      })
    }
  },

  /**
   * 
   */
  // initRoleView:function() {
  //   this.setData({
  //     role: userModel.getKlassRoleName(klassModel.getCurrentClass())
  //   })
  // },

  initMyNoticesView:function() {
    let result = noticeModel.makeUINotices(noticeModel.getMyNotices(),5)
    this.setData({
      notices:result
    })
  },

  /**
   * 点击通知列表事件处理函数
   */
  onShowMessagePreview: function (event) {
    getApp().globalData.param = event.currentTarget.dataset.notice;
    wx.navigateTo({
      url: '../noticePreview/noticePreview',
    })
  },

  /**
   * 点击查看所有事件处理函数
   */
  onShowAllMessageList:function() {
    wx.navigateTo({
      url: '../myAllNoticeList/myAllNoticeList'
    })
  },

  /**
   * 点击我的班级事件处理函数
   */
  onMyKlass:function(event) {
    getApp().globalData.param = event.currentTarget.dataset.klass
    console.log(getApp().globalData.param)
    wx.navigateTo({
      url: '../myClass/myClass'      
    })
  },

  /**
   * 更新用户信息
   */
  getUserInfo:function() {
    var that = this
    
    if (getApp().globalData.userInfo) {
      getApp().updateUserInfo(function (userInfo) {
        if (userInfo) {
          that.initClassView()
          // that.initRoleView()
        }

        wx.stopPullDownRefresh()
      })
    }else {
      wx.stopPullDownRefresh()
    }
  },

  /**
  * 获取我的通知列表数据
  */
  getNotices: function () {
    var that = this;
    request.getRequest('/notices', {}, true)
      .then(res => {
        var data = res.data;
        if (request.SUCCESSED == data.status) {
          var result = noticeModel.makeUINotices(res.data.items,5)
          noticeModel.setMyNotices(res.data.items)
          that.setData({
            notices: result,
            totalNoticeCount: res.data.items.length
          })
        } else {
          wx.showToast({
            title: data.msg,
            iocn: 'none'
          })
        }
      })
      .catch(e => {
        console.log(e)
      })
  },

  // onSelectRole:function() {
  //   var role = userModel.getRole(),url;
    
  //   if (userModel.NORMAL == role.role) {  // 未绑定身份
  //     url = '../selectRole/selectRole?klassSid=' + klassModel.getCurrentClass().sid; 
  //   } else if (userModel.HEAD_TEACHER == role.role || userModel.TEACHER == role.role) {
  //     getApp().globalData.param = role.teacher
  //     url = '../teacherInfo/teacherInfo' 
  //   } else if (userModel.PATRIARCH == role.role) {
  //     getApp().globalData.param = role.relations[0];
  //     // url = '../myStudents/myStudents';
  //     url = '../myStudentInfo/myStudentInfo'
  //   }

  //   wx.navigateTo({
  //     url: url,
  //   })
  // },

  refreshTopMessageView:function() {
    topMsg.showBindClassView(this)
  }
})